package com.lsp.microorder.pay.controller;


import com.lsp.microcommon.annotation.NoLogin;
import com.lsp.microcommon.common.CommonResult;
import com.lsp.microorder.pay.param.TradePayParam;
import com.lsp.microorder.pay.service.TradeOrderPayService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/app/order")
public class TradeOrderPayController {

    @Resource
    private TradeOrderPayService tradeOrderPayService;

    @PostMapping("/pay")
    public CommonResult tradeOrder(@RequestBody TradePayParam param) {
        return CommonResult.success(tradeOrderPayService.tradeOrderPay(param));
    }

    @NoLogin
    @PostMapping("/notify")
    public void notify(HttpServletRequest request, HttpServletResponse response) {
        tradeOrderPayService.payNotify(request, response);
    }

    @GetMapping("/pay/return")
    public CommonResult returnNotify(HttpServletRequest request, HttpServletResponse response) {
        tradeOrderPayService.payReturnUrl(request, response);
        return CommonResult.success();
    }

    //    查看交易订单号
    @GetMapping("/pay/states")
    public CommonResult tradePayStates(String orderNo, Integer payType) {
        try {
            tradeOrderPayService.payStates(orderNo, payType);
            return CommonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.error(600,e.getMessage());
        }

    }

    //    关闭交易订单号
    @GetMapping("/pay/close")
    public CommonResult tradePayClose(String orderNo, Integer payType) {
        tradeOrderPayService.closePay(orderNo, payType);
        return CommonResult.success();
    }


}
